package com.ruoyi.project.channel.businessRate.controller;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.project.channel.payerBusinessInfo.domain.PayerBusinessInfo;
import com.ruoyi.project.channel.payerBusinessInfo.service.IPayerBusinessInfoService;
import com.ruoyi.project.channel.payerPayType.domain.PayerPayType;
import com.ruoyi.project.channel.payerPayType.service.IPayerPayTypeService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.channel.businessRate.domain.BusinessRate;
import com.ruoyi.project.channel.businessRate.service.IBusinessRateService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 商户对应通道费率信息操作处理
 *
 * @author zero
 * @date 2019-05-12
 */
@Controller
@RequestMapping("/channel/businessRate")
public class BusinessRateController extends BaseController {
    private String prefix = "channel/businessRate";
    @Autowired
    private IUserService userService;
    @Autowired
    private IBusinessRateService businessRateService;
    @Autowired
    private IPayerPayTypeService payerPayTypeService;
    @Autowired
    private IPayerBusinessInfoService payerBusinessInfoService;

    @RequiresPermissions("channel:businessRate:view")
    @GetMapping("/{businessId}")
    public String businessRate(@PathVariable("businessId") Long businessId, ModelMap mmap) {
        mmap.put("businessId", businessId);

        return prefix + "/businessRate";
    }

    @RequiresPermissions("channel:businessRate:view")
    @GetMapping("/channel/{channelId}")
    public String settingChanel(@PathVariable("channelId") Long channelId, ModelMap mmap) {
        mmap.put("channelId", channelId);
        User user = new User();
        user.setDeptId(120L);
        List<User> users=  userService.selectUserList(user);
        mmap.put("users", users);
        return prefix + "/businessSettingRate";
    }

    @GetMapping("/allInfo")
    @ResponseBody
    public AjaxResult getAllInfo() {
        JSONObject jsonObject = new JSONObject();
        List<PayerPayType> payerPayTypes = payerPayTypeService.selectPayerPayTypeList(new PayerPayType());
        jsonObject.put("payerPayTypes", payerPayTypes);
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(new PayerBusinessInfo());
        for(int i=0;i<payerBusinessInfos.size();i++){
            payerBusinessInfos.get(i).setApiKey("");
        }
        jsonObject.put("payerBusinessInfos", payerBusinessInfos);
        List<User> user = userService.selectPayerlList(new User());
        jsonObject.put("payers",user);
        return AjaxResult.success("获取成功", jsonObject);
    }

    /**
     * 查询商户对应通道费率列表
     */
    @RequiresPermissions("channel:businessRate:list")
    @PostMapping("/list/{businessId}")
    @ResponseBody
    public TableDataInfo list(BusinessRate businessRate, @PathVariable("businessId") Long businessId) {
        startPage();
        businessRate.setBusinessId(businessId);
        List<BusinessRate> list = businessRateService.selectBusinessRateList(businessRate);
        return getDataTable(list);
    }

    /**
     * 查询商户对应通道费率列表
     */
    @RequiresPermissions("channel:businessRate:list")
    @PostMapping("/list/channel/{channelId}")
    @ResponseBody
    public TableDataInfo channelList(BusinessRate businessRate, @PathVariable("channelId") Long channelId) {
        startPage();
        businessRate.setChannelId(channelId);
        List<BusinessRate> list = businessRateService.selectBusinessRateList(businessRate);
        return getDataTable(list);
    }


    /**
     * 导出商户对应通道费率列表
     */
    @RequiresPermissions("channel:businessRate:export")
    @PostMapping("/export/{businessId}")
    @ResponseBody
    public AjaxResult export(BusinessRate businessRate, @PathVariable("businessId") Long businessId) {
        businessRate.setBusinessId(businessId);
        List<BusinessRate> list = businessRateService.selectBusinessRateList(businessRate);
        ExcelUtil<BusinessRate> util = new ExcelUtil<BusinessRate>(BusinessRate.class);
        return util.exportExcel(list, "businessRate");
    }

    /**
     * 新增商户对应通道费率
     */
    @GetMapping("/add/{businessId}")
    public String add(@PathVariable("businessId") Long businessId, ModelMap mmap) {
        mmap.put("businessId", businessId);
        List<User> user2 = userService.selectChannelList(new User());
        mmap.put("channels",user2);
        return prefix + "/add";
    }

    /**
     * 新增保存商户对应通道费率
     */
    @RequiresPermissions("channel:businessRate:add")
    @Log(title = "商户对应通道费率", businessType = BusinessType.INSERT)
    @PostMapping("/add/{businessId}")
    @ResponseBody
    public AjaxResult addSave(BusinessRate businessRate,@PathVariable("businessId") Long businessId) {
//        BusinessRate search = new BusinessRate();
//        search.setBusinessId(businessId);
//        search.setPayType(businessRate.getPayType());
//        List<BusinessRate> rates = businessRateService.selectBusinessRateList(search);
//        if(rates.size() > 0){
//            rates.get(0).setPayRate(businessRate.getPayRate());
//            return toAjax(businessRateService.updateBusinessRate(rates.get(0)));
//        }

        businessRate.setBusinessId(businessId);
        return toAjax(businessRateService.insertBusinessRate(businessRate));
    }

    /**
     * 修改商户对应通道费率
     */
    @GetMapping("/edit/{rateId}")
    public String edit(@PathVariable("rateId") Long rateId, ModelMap mmap) {
        BusinessRate businessRate = businessRateService.selectBusinessRateById(rateId);
        mmap.put("businessRate", businessRate);
        List<User> user2 = userService.selectChannelList(new User());
        mmap.put("channels",user2);
        return prefix + "/edit";
    }


    /**
     * 修改保存商户对应通道费率
     */
    @RequiresPermissions("channel:businessRate:edit")
    @Log(title = "商户对应通道费率", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BusinessRate businessRate) {
        return toAjax(businessRateService.updateBusinessRate(businessRate));
    }

    /**
     * 修改保存商户对应通道费率
     */
    @RequiresPermissions("channel:businessRate:edit")
    @Log(title = "商户对应通道费率", businessType = BusinessType.UPDATE)
    @PostMapping("/edit/channel")
    @ResponseBody
    public AjaxResult settingChannel(String ids,String useStatus) {
        return toAjax(businessRateService.setBusinessRateStatusByIds(ids,useStatus));
    }

    /**
     * 删除商户对应通道费率
     */
    @RequiresPermissions("channel:businessRate:remove")
    @Log(title = "商户对应通道费率", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(businessRateService.deleteBusinessRateByIds(ids));
    }
}
